package com.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hdb.DB;
import com.hdb.TAdmin;
import com.hdb.dao.Hib;
import com.util.DAOContainer;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpSession;
import java.lang.reflect.*;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

/**
 * Created by aszer on 2016/11/12.
 */
abstract class BasicService {
    abstract Hib getDAO();

    public static final int ReadLevel = 1;
    public static final int AttachLevel = 2;
    public static final int DeleteLevel = 3;

    public DB db;

    /*查找所有*/
    public String query(int start, int length) {
        if (length > 60) length = 60;
        Hib dao = getDAO();
        if (!checkPermission(ReadLevel) || dao == null) return "";
        List result = dao.findAll(start, length);
        return sendJSON(result);
    }

    /*更新*/
    public Boolean attach(Map<String, String> s) {
        try {
            Hib dao = getDAO();
            if (!checkPermission(AttachLevel) || dao == null) return false;

            Object table = dao.findById(Integer.parseInt(s.get("id")));

            Object i = DAOContainer.updateT(table, dao.getTClass(), s);
            if (i == null) return false;
            boolean res = dao.attach(i);
            return res;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public Boolean myDelete(int id) {
        try {
            Hib dao = getDAO();
            if (!checkPermission(DeleteLevel) || dao == null) return false;
            Object i = dao.delete(dao.getSession().load(dao.getClass(), id));
            return i != null && dao.delete(i);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public String sendJSON(Object object) {
        ObjectMapper json = new ObjectMapper();
        try {
            return json.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return "";
    }

    public boolean checkPermission(int level) {
        boolean[] permission = {true, true, true};//(boolean[])getSession().getAttribute("permission");
        if (permission == null) return false;
        switch (level) {
            case ReadLevel:
                return permission[0];
            case AttachLevel:
                return permission[1];
            case DeleteLevel:
                return permission[2];
            default:
                return false;
        }
    }

    public HttpSession getSession() {
        return ServletActionContext.getRequest().getSession();
    }

    public DB getDb() {
        return db;
    }

    public void setDb(DB db) {
        this.db = db;
    }

    class E {
        private String EType = null;
        private String EInfo = null;
        private String EMessage = null;
        private String EJump = null;

        public E() {
        }

        public E(String EMessage) {
            this.EMessage = EMessage;
        }

        public E(String EMessage, String EJump) {
            this.EMessage = EMessage;
            this.EJump = EJump;
        }

        public String getEType() {
            return EType;
        }

        public void setEType(String EType) {
            this.EType = EType;
        }

        public String getEInfo() {
            return EInfo;
        }

        public void setEInfo(String EInfo) {
            this.EInfo = EInfo;
        }

        public String getEMessage() {
            return EMessage;
        }

        public void setEMessage(String EMessage) {
            this.EMessage = EMessage;
        }

        public String getEJump() {
            return EJump;
        }

        public void setEJump(String jump) {
            this.EJump = jump;
        }
    }
}
